const http = require("http");
const fs = require("fs");
const path = require("path");

var pagePath = path.join(__dirname,"./index.html");
var iconPath = path.join(__dirname,"./icon.png");
function parseUrl(urlStr){
    var index = urlStr.indexOf("?");
    var pathName = "";
    var args = "";
    var argObj = {};
    if(index == -1){
        pathName = urlStr;
    }
    else{
        pathName = urlStr.slice(0,index);
        args = urlStr(index+1);
        var argArr = args.split("&");
        for(var i = 0;i<argArr.length;i++){
            argObj[argArr[i].split("=")[0]] = argArr[i].split("=")[1];
        }
        console.log(argObj);
    }
    return {
        pathName:pathName,
        argObj:argObj
    }
}

function router(urlObj,req,res){
    if(urlObj.pathName.indexOf(".png") > 0 || urlObj.pathName.indexOf(".jpg") > 0){
        urlObj.pathName = "getimgdata";
    }
    switch(urlObj.pathName){
        case "/":
            showIndex(res);
            break;
        case "getimgdata":
            resImage(res);
            break;
        case "/getList":
            showList(res);
            break;
        case "/del":
            deleteFile(urlObj.argObj,res);
            break;
    }
}

function showIndex(res){
    var fileContent = fs.readFileSync(pagePath);
    res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
    res.write(fileContent);
    res.end();
}

function resImage(res){
    var imgContent = fs.readFileSync(iconPath);
    res.writeHead(200,{"Content-Type":"text/html;charset=utf8"})
    res.write(imgContent);
    res.end()
}

function showList(res){
    var dirPath = path.join(__dirname,"/fileDir");
    var files = fs.readdirSync(dirPath);
    for(var i=0; i < files.length;i++){
        var filePath = path.join(dirPath,"/" + files[i]);
        var fileObj = fs.statSync(filePath);//获取文件信息 看是文件还是目录
        var obj = {};
        obj.fileName = files[i];
        obj.createTime = fileObj.ctimeMs;
        obj.fileSize = fileObj.size;
        obj.fileType = fileObj.isFile() ? "file" : "folder";
        fileList.push(obj);
    }
}

var jsonStr = JSON.stringify(fileList);
res.writeHead(200,{"Content-Type":"text/html;charset=utf8"})
res.write(jsonStr);
res.end();


function deleteFile(argObj,res){
    var delId = argObj.delId;
    var dirPath = path.join(__dirname,"/fileDir");
    var files = fs.statSync(path.join(dirPath,"/"+files[i]));
    for(var i = 0;i<files.length;i++){
        var fileObj = fs.statSync(path,join(dirPath,"/" + files[i]));
        if(delId.indexOf(fileObj.ctimeMs) > 0){
            //这应该写递归删除的那个代码
            fs.unlikeSync(path.join(dirPath,"/" + files[i]))
            res.end("success");
            break;
        }
    }
}
http.createServer((req,res) => {
    /**
     * 
     */
    var urlStr = req.url;
    var urlObj = parseUrl(urlStr);
    router(urlObj,req,res);
}).listen(8081)

console.log("server is listening 8081")